package com.qianguo.sender.tab;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eva.androidlibrary.api.entity.UserInfo;
import com.qianguo.sender.R;
import com.qianguo.sender.api.entity.SenderUserInfo;
import com.qianguo.sender.base.BaseFragment;
import com.qianguo.sender.usercenter.CertificationActivity;
import com.qianguo.sender.usercenter.ProfileActivity;
import com.qianguo.sender.usercenter.SenderAccountActivity;
import com.qianguo.sender.usercenter.SenderRecommendShopActivity;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * @author eva
 * @date 16/4/29
 */
@EFragment(R.layout.fragment_sender_profile)
public class SenderProfileFragment extends BaseFragment {

    /** 身份认证按钮 */
    @ViewById(R.id.sender_profile_identity_authentication_button)
    Button mBtnIdentify;

    /** 用户已认证，用户信息块 */
    @ViewById(R.id.sender_profile_info_layout)
    RelativeLayout mProfileLayout;

    /** 配送单数 */
    @ViewById(R.id.sender_profile_delivery_count_tv)
    TextView mTvDeliveryCount;

    /** 评论数 */
    @ViewById(R.id.sender_profile_comment_count_tv)
    TextView mTvCommentCount;

    /** 今日订单数 */
    @ViewById(R.id.sender_profile_order_tv)
    TextView mTvOrderCount;

    /** 账户 */
    @ViewById(R.id.sender_profile_account_tv)
    TextView mTvAccount;

    /** 头像 */
    @ViewById(R.id.sender_profile_avatar_image_view)
    ImageView mImgAvatar;

    private SenderUserInfo mUser;

    @Click(R.id.sender_profile_account_tv)
    void clickAccount() {
        SenderAccountActivity.jumpToAccount(getActivity());
    }

    @Click(R.id.sender_profile_more_tv)
    void clickMore() {
        // TODO: 16/5/11 跳转到更多
    }

    @Click(R.id.sender_profile_info_layout)
    void clickProfile() {
        ProfileActivity.jumpToProfile(getActivity());
    }

    void clickRecommend() {
        SenderRecommendShopActivity.jumpToRecommend(getActivity());
    }

    @Override
    protected void initializeUI() {
        getUserInfo();
    }

    @Background
    void getUserInfo() {
        mUser = new SenderUserInfo();
        mUser.setNickname("Eva");
        mUser.setBalance(90);
        mUser.setCommentNum(20);
        mUser.setDeliveryNum(20);
        mUser.setIsRealName(UserInfo.AUTH_STATUS_YES);

        refreshProfile();
    }

    @UiThread
    void refreshProfile() {
        if (mUser.isCertification()) {
            // 已认证
            mProfileLayout.setVisibility(View.VISIBLE);
            mBtnIdentify.setVisibility(View.GONE);
            mTvDeliveryCount.setText(getString(R.string.sender_profile_delivery_num, mUser.getDeliveryNum()));
            mTvCommentCount.setText(getString(R.string.sender_profile_comment_num, mUser.getCommentNum()));
            mImageLoader.loadImage(getActivity(), mImgAvatar, mUser.getAvatar(), R.mipmap.default_avatar);
            mTvOrderCount.setText(getString(R.string.sender_profile_orders, mUser.getOrderNum()));
            mTvAccount.setText(getString(R.string.sender_profile_account, mUser.getBalance()));
        } else {
            mProfileLayout.setVisibility(View.GONE);
            mBtnIdentify.setVisibility(View.VISIBLE);
            mBtnIdentify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 16/5/16 跳转到实名认证页面
                    CertificationActivity.jumpToCertification(getActivity());
                }
            });
        }
    }
}
